<template>
  <div ref="unitselect" id="unitselect">
    <h1 style="text-align:center;background-color: #1FA1F9;color: white;height:80px;line-height:80px;margin:0;">机构控烟状况调查</h1>
    <el-row style="margin: 0;">
      <el-col :span="24">
        <img src="../assets/nosmoking.png" width="130" height="130" />
      </el-col>
    </el-row>
    <el-row style="margin-top: 20px;">
      <el-col :span="24" style="text-align:left;">
        <span style="font-size: 18px;">请选择本次受调查机构名称：</span>
      </el-col>
    </el-row>
    <el-row>
      <el-col class="rowCls" :span="7"><span>单位类型：</span></el-col>
      <el-col :span="16">
        <el-select name="unittype" v-model="unittype" value-key="type" @change="unitTypeSelect" >
          <el-option v-for="item in unitlst" :key="item.type" :value="item" :label="item.name"></el-option>
        </el-select>
      </el-col>
    </el-row>
    <el-row>
      <el-col class="rowCls" :span="7">单位名称：</el-col>
      <el-col :span="16">
        <el-select name="unitgroup" v-model="unitgroup" value-key="type" @change="groupchange" >
          <el-option v-for="item in unittype.groups" :key="item.type" :value="item" :label="item.name"></el-option>
        </el-select>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="7">&nbsp;</el-col>
      <el-col :span="16">
        <el-select name="unit" v-model="unit" value-key="id" @change="unitchange">
          <el-option v-for="item in unitgroup.units" :key="item.id" :value="item" :label="item.name"></el-option>
        </el-select>
      </el-col>
    </el-row>
    <section>
      <button type="primary" @click="doQuestnaire">填写调查详情</button>
    </section>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  name: 'unitselect',
  data () {
    return {
      unittype: {},
      unitgroup: {},
      unit: {},
      unitlst: []
    }
  },
  mounted () {
    this.loadUnits()
  },
  methods: {
    loadUnits () {
      var _that = this
      axios.get('/static/mock/unit.json').then((res) => {
        _that.unitlst = res.data
        _that.resetDefault(0)
      })
    },
    resetDefault (type) {
      if (type === 0) {
        this.unittype = this.unitlst[0]
      }
      if (type <= 1) {
        this.unitgroup = this.unittype.groups[0]
        this.unitarea = this.unittype.areas[0]
      }
      if (type <= 2) {
        this.unit = this.unitgroup.units[0]
      }
    },
    unitTypeSelect (val) {
      this.resetDefault(1)
    },
    groupchange (val) {
      this.resetDefault(2)
    },
    unitchange (val) {
    },
    doQuestnaire () {
      this.$router.push({name: 'Home', params: {typeid: this.unittype.type, groupid: this.unitgroup.type, unitid: this.unit.id}})
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
@import "../assets/css/global.css";
.unitselect {
  font-size: 1.4em;
  text-align: left;
  background-color: #F8F6F9;
}

button {
  display: block;
  margin: 30px auto 0 auto;
  height: 50px;
  background-color: rgba(51, 125, 253, 0.93);
  outline: none;
  border: none;
  border-radius: 5px;
  color: white;
  font-size: 1.5em;
}
</style>
